extends "res://BaseEnemy.gd"

# 精英森林巨魔特有属性
@export var smash_cooldown = 6.0  # 重击冷却时间
@export var smash_damage = 25  # 重击伤害
@export var smash_radius = 80.0  # 重击范围
@export var regeneration_rate = 2  # 每秒恢复生命值
@export var enrage_health_threshold = 0.3  # 激怒阈值（剩余生命百分比）

var smash_timer = 0.0
var can_smash = true
var is_enraged = false
var regen_timer = 0.0

func _ready():
	# 调用父类的_ready方法
	super._ready()
	
	# 设置精英森林巨魔特有属性
	max_health = 180
	current_health = max_health
	movement_speed = 70.0
	damage = 18
	experience_value = 35
	is_elite = true
	
	# 更新生命值条
	if health_bar:
		health_bar.max_value = max_health
		health_bar.value = current_health
	
	# 设置外观
	if sprite:
		sprite.modulate = Color(0.6, 1.0, 0.6)  # 精英巨魔颜色

func _physics_process(delta):
	# 调用父类的_physics_process方法
	super._physics_process(delta)
	
	# 处理重击冷却
	if not can_smash:
		smash_timer += delta
		if smash_timer >= smash_cooldown:
			can_smash = true
			smash_timer = 0.0
	
	# 生命恢复
	regen_timer += delta
	if regen_timer >= 1.0:  # 每秒恢复
		regen_timer = 0.0
		if current_health < max_health:
			current_health = min(current_health + regeneration_rate, max_health)
			if health_bar:
				health_bar.value = current_health
	
	# 如果可以重击且玩家存在，尝试重击
	if can_smash and target_player and is_instance_valid(target_player):
		var distance = global_position.distance_to(target_player.global_position)
		if distance <= 100:  # 在适当距离时使用重击
			smash_attack()

func smash_attack():
	if animation_player and animation_player.has_animation("smash"):
		animation_player.play("smash")
		
		# 等待动画中点（实际打击时刻）
		await get_tree().create_timer(0.5).timeout
		
		# 创建重击效果
		var smash_effect = preload("res://SmashEffect.tscn").instantiate()
		get_tree().get_root().add_child(smash_effect)
		smash_effect.global_position = global_position
		
		# 检测范围内的玩家
		if target_player and is_instance_valid(target_player):
			var distance = global_position.distance_to(target_player.global_position)
			if distance <= smash_radius:
				# 对玩家造成伤害
				var actual_damage = smash_damage
				if is_enraged:
					actual_damage *= 1.5
				
				target_player.take_damage(actual_damage)
				
				# 击退效果
				var knockback_direction = (target_player.global_position - global_position).normalized()
				if target_player.has_method("apply_knockback"):
					target_player.apply_knockback(knockback_direction * 250)
				
				print("巨魔的重击造成", actual_damage, "伤害!")
		
		# 重置重击冷却
		can_smash = false
		smash_timer = 0.0
		
		print("精英巨魔使用重击!")

# 重写受伤方法，添加激怒逻辑
func take_damage(amount):
	super.take_damage(amount)
	
	# 检查是否需要激怒
	if not is_enraged and current_health <= max_health * enrage_health_threshold:
		enter_enrage()

func enter_enrage():
	is_enraged = true
	
	if animation_player and animation_player.has_animation("enrage"):
		animation_player.play("enrage")
	
	# 增强属性
	movement_speed *= 1.3
	damage *= 1.5
	regeneration_rate *= 2
	
	# 视觉效果
	if sprite:
		sprite.modulate = Color(1.0, 0.5, 0.5)  # 愤怒的红色
	
	print("精英巨魔进入激怒状态!")

# 重写攻击方法
func attack_player():
	if is_enraged and randf() < 0.4:  # 激怒状态下40%几率使用重击
		smash_attack()
	else:
		# 普通攻击
		super.attack_player()
